package pcjavanet.mobilesell.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import pcjavanet.mobilesell.model.Department;
import pcjavanet.mobilesell.model.DepartmentExample;
import pcjavanet.mobilesell.model.Employee;
import pcjavanet.mobilesell.model.EmployeeExample;
import pcjavanet.mobilesell.model.PageBean;
import pcjavanet.mobilesell.model.QryCondition;
import pcjavanet.mobilesell.model.Shop;
import pcjavanet.mobilesell.model.ShopExample;
import pcjavanet.mobilesell.persistence.DepartmentMapper;
import pcjavanet.mobilesell.persistence.EmployeeMapper;
import pcjavanet.mobilesell.persistence.ShopMapper;
import pcjavanet.util.AES;


@Service
public class EmployeeService {


  @Autowired
  private  EmployeeMapper employeeMapper;
  @Autowired
  private  DepartmentMapper departmentMapper;
  @Autowired
  private  ShopMapper shopMapper;


  public int  insert( Employee employee ) throws Exception{
	  if ( employee.getPassword() != null && employee.getPassword().length() <= 15 ){
		  employee.setPassword(  AES.createPassword( employee.getPassword() ) );
	  }
	  employee.setCreateDate( new Date() );
	return employeeMapper.insert (employee);
  }


  public Employee  selectByPrimaryKey( Employee  bean ) {
 	int dbid = bean.getDbid();
	Employee employee = employeeMapper.selectByPrimaryKey( dbid );
	Department department = departmentMapper.selectByPrimaryKey( employee.getRefDepartmentId() );
	employee.setDepartment(department) ;
	return employee ;
  }


  public int  updateByPrimaryKeySelective( Employee employee ) throws Exception{
	  if ( employee.getPassword() != null && employee.getPassword().length() <= 15 ){
		  employee.setPassword(  AES.createPassword( employee.getPassword() ) );
	  }
	  return employeeMapper.updateByPrimaryKeySelective( employee );
  }


  public int  deleteByPrimaryKey( Employee  bean ) throws Exception{
 	int dbid = bean.getDbid();
	return employeeMapper.deleteByPrimaryKey( dbid );
  }


  public List<Employee>  selectByExample( Employee employee ) throws Exception{
    EmployeeExample example = new EmployeeExample();
	return employeeMapper.selectByExample( example );
  }

  public List<Employee>  getOnePageBeans( QryCondition condition , PageBean pageBean ) throws Exception{
    EmployeeExample example = new EmployeeExample();
    ServiceUtil.dealCriteria( condition,  example.createCriteria() );
	List<Employee> ls=employeeMapper.getOnePageBeans( example , pageBean );
//begin
	if ( ls.size() > 0 ) {
  		Map<Integer,Integer> departmentIdmap = new HashMap<Integer,Integer>();
  		for(Employee  m : ls){
  			m.setDepartment(new Department ()) ; 
  			if ( m.getRefDepartmentId() != null ) 
  				departmentIdmap.put(m.getRefDepartmentId(), m.getRefDepartmentId());
  		};
  		List<Integer> refDepartmentIds = new ArrayList<Integer>();
  		Iterator<Integer> refDepartmentIdit = departmentIdmap.keySet().iterator(); 
  		while ( refDepartmentIdit.hasNext() ){
  			refDepartmentIds.add( refDepartmentIdit.next() );
  		}
  		DepartmentExample departmentExample = new DepartmentExample();
  		if ( refDepartmentIds.size() > 0 ) 
  			departmentExample.createCriteria().andDbidIn( refDepartmentIds );
  		List<Department> departments = departmentMapper.selectByExample( departmentExample);
  		for(Employee m : ls ){
  			for(Department c : departments ){
  				if ( m.getRefDepartmentId() !=null && c.getDbid() !=null && m.getRefDepartmentId().intValue() == c.getDbid().intValue() ) {
  					m.setDepartment( c );
  				}
  			}
  		}
  		//for bean.setRefShopDbid(refShopDbid);
  		Map<Integer,Integer> refShopIdmap = new HashMap<Integer,Integer>();
  		for(Employee  m : ls){
  			m.setShop(new Shop ()) ; 
  			if ( m.getRefShopId() != null ) 
  				refShopIdmap.put(m.getRefShopId(), m.getRefShopId());
  		};
  		List<Integer> refShopIds = new ArrayList<Integer>();
  		Iterator<Integer> refShopIdit = refShopIdmap.keySet().iterator(); 
  		while ( refShopIdit.hasNext() ){
  			refShopIds.add( refShopIdit.next() );
  		}
  		ShopExample shopmentExample = new ShopExample();
  		if ( refShopIds.size() > 0 ) 
  			shopmentExample.createCriteria().andDbidIn( refShopIds );
  		List<Shop> shops = shopMapper.selectByExample( shopmentExample );
  		for(Employee m : ls ){
  			for(Shop c : shops ){
  				if ( m.getRefShopId() !=null && c.getDbid() !=null && m.getRefShopId().intValue() == c.getDbid().intValue() ) {
  					m.setShop( c );
  				}
  			}
  		}
	}
//end
	return ls;
  }

  public int getTotalRecords( QryCondition condition , PageBean pageBean ) throws Exception{
    EmployeeExample example = new EmployeeExample();
    ServiceUtil.dealCriteria( condition,  example.createCriteria() );
	return employeeMapper.getTotalRecords( example , pageBean );
  }

  public int countByExample( Employee employee , PageBean pageBean ){
    EmployeeExample example = new EmployeeExample();
	return employeeMapper.countByExample( example );
  }
  
  public List<Employee> selectByExamplePage( Employee employee , PageBean pageBean ){
	  EmployeeExample example = new EmployeeExample();
	return employeeMapper.selectByExamplePage( example , pageBean );
  }
  
  public List<Employee> checkLogin( Employee employee ){
	  EmployeeExample example = new EmployeeExample();
	  String password = AES.createPassword(employee.getPassword());
	  example.createCriteria().andNameEqualTo(employee.getName()).andPasswordEqualTo(password);
	  return employeeMapper.selectByExample( example );
  }
  
  public List<Employee> checkByDbidAndPassword( Employee employee ){
	  EmployeeExample example = new EmployeeExample();
	  String password = AES.createPassword(employee.getPassword());
	  example.createCriteria().andDbidEqualTo( employee.getDbid() ).andPasswordEqualTo(password);
	  return employeeMapper.selectByExample( example );
  }

}
